package com.example.lj.uav.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.lj.uav.R;

public class BatteryView extends View {

    public static final int STROKE_WIDTH = 3;
    private Context context;
    private int mPower;

    private Paint batteryOutlinesPaint; // 电池外框的画笔
    private Paint batteryPaint; // 电池内部画笔

    private int battery_width; // 电池的宽
    private int battery_height; // 电池的高
    private int battery_inside_margin; // 电池内框距外框的margin


    public BatteryView(Context context) {
        super(context);
    }

    public BatteryView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.BatteryView);
        battery_width = array.getInteger(R.styleable.BatteryView_battery_view_width, 120);
        battery_height = array.getInteger(R.styleable.BatteryView_battery_view_height, 160);
        battery_inside_margin = array.getInteger(R.styleable.BatteryView_battery_view_inside_margin, 8);
        array.recycle();
        init(context);
    }

    public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.BatteryView);
        battery_width = array.getInteger(R.styleable.BatteryView_battery_view_width, 120);
        battery_height = array.getInteger(R.styleable.BatteryView_battery_view_height, 160);
        battery_inside_margin = array.getInteger(R.styleable.BatteryView_battery_view_inside_margin, 8);
        array.recycle();
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        batteryOutlinesPaint = new Paint();
        batteryOutlinesPaint.setColor(getResources().getColor(R.color.gray_light));
        batteryOutlinesPaint.setAntiAlias(true);
        batteryOutlinesPaint.setStrokeWidth(STROKE_WIDTH);
        batteryOutlinesPaint.setStyle(Paint.Style.STROKE);

        batteryPaint = new Paint();
        batteryPaint.setColor(Color.GREEN);
        batteryPaint.setAntiAlias(true);
        batteryPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int rootHeight = getMeasuredHeight(); // 画布高度
        int rootWidth = getMeasuredWidth();// 画布宽度

        int left = (rootWidth-battery_width)/2;
        int top = (rootHeight-battery_height)/2;
        int right = battery_width+left;
        int bottom = battery_height+top;

        RectF outLineRect = new RectF(left,top,right,bottom);
        canvas.drawRoundRect(outLineRect,6,6,batteryOutlinesPaint);


        //画电量
        float power_percent = mPower/100.0f;
        if (mPower != 0) {
            int powerLeft = battery_inside_margin+left;
            int powerRight = battery_width-battery_inside_margin+left;
            int powerFullWidth = battery_height - battery_inside_margin * 2;
            int powerTop = (int) (battery_height-battery_inside_margin-powerFullWidth*power_percent+top);
            int powerBottom = battery_height - battery_inside_margin+top;
            RectF batteryRect = new RectF(powerLeft, powerTop, powerRight, powerBottom);
            canvas.drawRoundRect(batteryRect, 2, 2, batteryPaint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(battery_width + STROKE_WIDTH * 2
                , battery_height + STROKE_WIDTH * 2);
    }

    public void setPower(int power){
        this.mPower = power;
        if (mPower<0){
            mPower=0;
        }
        invalidate();
    }
}
